#include <algorithm>
#include <GL/freeglut.h>
#include "PlayerEntity.h"
#include "main.h"
#include <iostream>

PlayerEntity::PlayerEntity():
m_energy(1000)
{

}

PlayerEntity::~PlayerEntity()
{

}

void PlayerEntity::Render()
{
	glPointSize(2.0);
	glBegin(GL_POINTS);
	glColor3f(0.0, 1.0, 1.0);
	glVertex2i(m_pos.x, m_pos.y);
	glEnd();
}

void PlayerEntity::Tick()
{

}

void PlayerEntity::AddToEnergy(int i)
{
	m_energy = std::min(m_energy + i, MAX_ENERGY);
}

void PlayerEntity::SubtractFromEnergy(int i)
{
	m_energy = std::max(m_energy - i, MIN_ENERGY);
}

void PlayerEntity::KeyboardInput(unsigned char c, int x, int y)
{

}

void PlayerEntity::ProcessSpecialInput(int key, int x, int y)
{
	MoveDirection dir;
	switch (key)
	{
	case GLUT_KEY_DOWN:
		dir = MOVE_DOWN;
		break;
	case GLUT_KEY_UP:
		dir = MOVE_UP;
		break;
	case GLUT_KEY_LEFT:
		dir = MOVE_LEFT;
		break;
	case GLUT_KEY_RIGHT:
		dir = MOVE_RIGHT;
		break;
	}
	GetWorldManager()->MoveEntity(this, dir);
}

bool PlayerEntity::Move(const MoveInfo &m)
{
	using namespace std;
	if (m_energy > m.GetCost())
	{
		m_pos.x = m.GetDesitination().x;
		m_pos.y = m.GetDesitination().y;
		SubtractFromEnergy(m.GetCost());
		return true;
	}

	return false;
}

void PlayerEntity::Collide(Entity * ent)
{
	int cost = ent->GetCollideCost();
	if (m_energy > cost)
	{
		SubtractFromEnergy(cost);
		// destroy the entity
		GetWorldManager()->RemoveEntity(ent);
	}
	else
	{
		GetGameManager()->EndGame(false);
	}
	return;
}

bool PlayerEntity::ConsumesUponsEntry()
{
	return true;
}